﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WebAuto.DataModel;

namespace WebAuto
{
    public partial class ChartData : Form
    {

        public string TestNameVar { get; set; }
        public string TestStatusVar { get; set; }


        public ChartData()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ChartData_Load(object sender, EventArgs e)
        {

            _fillDataGrid();

        }

        private void _fillDataGrid()
        {
            dataGridView1.Rows.Clear();
            Data data = new Data();
            var list = data.GetTestRunDetailsPerNameAndStatus(TestNameVar, TestStatusVar);
            foreach(var item in list)
            {
                dataGridView1.Rows.Add(item.ResultFolderLocation,item.TestName, item.RunDate, item.RunStatus, item.Message,item.Browser);

            }


        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
            if (dataGridView1.SelectedCells.Count > 0)
            {
               DataGridViewSelectedCellCollection col = this.dataGridView1.SelectedCells;
                if(col[0].Value==null)
                {
                    MessageBox.Show("The test results location is empty!");
                    return;
                }
               string selectedResultLocation = col[0].Value.ToString();
               if(!string.IsNullOrEmpty(selectedResultLocation))
               {
                   if (Directory.Exists(selectedResultLocation))
                   {
                   
                       _createImageScroller(selectedResultLocation);
                   }else
                   {
                       MessageBox.Show("The test results location could not be found");
                   }

               }else
               {
                   MessageBox.Show("No results folder found");

               }
            }
        }

        private void _createImageScroller(string location)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo("explorer.exe");
            startInfo.Arguments = " "+location.Replace(@"\\",@"\");
            Process.Start(startInfo);     

        }

       

    }
}
